import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router';
const login = () => import('@/views/login/index.vue');
const index = () => import('@/views/index/index.vue');
const hindex = () => import('@/views/home/index.vue');
const simple = () => import('@/views/tabel/simple.vue');
const complex = () => import('@/views/tabel/complex.vue');
const about = () => import('@/views/about/index.vue');
const rich = () => import('@/views/rich/index.vue');
const map = () => import('@/views/map/index.vue');
const img = () => import('@/views/other/img.vue');
const pillar = () => import('@/views/chart/pillar.vue');
const brokenline = () => import('@/views/chart/brokenline.vue');
const other = () => import('@/views/chart/other.vue');
const routes: RouteRecordRaw[] = [
    {
        path: '/',
        redirect: '/login',
    },
    {
        path: '/login',
        name: 'login',
        component: login,
    },
    {
        path: '/index',
        name: 'index',
        component: index,
        redirect: '/hindex',
        children: [
            { path: '/hindex', name: 'hindex', component: hindex },
            { path: '/simple', name: 'simple', component: simple },
            { path: '/complex', name: 'complex', component: complex },
            { path: '/about', name: 'about', component: about },
            { path: '/rich', name: 'rich', component: rich },
            { path: '/map', name: 'map', component: map },
            { path: '/img', name: 'img', component: img },
            { path: '/pillar', name: 'pillar', component: pillar },
            { path: '/brokenline', name: 'brokenline', component: brokenline },
            { path: '/other', name: 'other', component: other },
        ],
    },
];

const router = createRouter({
    history: createWebHashHistory(), // 路由模式
    routes,
});

// 路由拦截
router.beforeEach((to, from, next) => {
    let token: string | null = localStorage.getItem('eleToken');
    if (!token && to.path == '/login') {
        next();
    } else if (!token && to.path != '/login') {
        next({ path: '/' });
    } else if (to.path !== '/login' && token) {
        next();
    } else if (to.path === '/login' && token) {
        next({ path: '/index' });
    }
});

export default router;
